﻿using System;
using System.Collections.Generic;

namespace Microsoft.Xna.Framework.Media
{
	/// <summary>
	/// Provides methods and properties to access and control the queue of playing songs.
	/// </summary>
	public sealed class MediaQueue
	{
		#region Fields
		private int currentSongIndex;
		private List<Song> songQueue;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the current Song in the queue of playing songs.
		/// </summary>
		public Song ActiveSong
		{
			get { return songQueue[currentSongIndex]; }
		}

		/// <summary>
		/// Gets or sets the index of the current (active) song in the queue of playing songs.
		/// </summary>
		public int ActiveSongIndex
		{
			get { return currentSongIndex; }
			set
			{
				if ((value < 0) || (value >= songQueue.Count))
				{
					throw new ArgumentOutOfRangeException("value");
				}
				ActiveSong.Stop();
				currentSongIndex = value;
				ActiveSong.Play();
			}
		}

		/// <summary>
		/// Gets the count of songs in the MediaQueue.
		/// </summary>
		public int Count
		{
			get { return songQueue.Count; }
		}

		internal TimeSpan PlayPosition
		{
			get
			{
				TimeSpan span = new TimeSpan(0L);
				if (ActiveSongIndex != -1)
				{
					span = songQueue[currentSongIndex].songElement.Position;
				}
				return span;
			}
		}

		/// <summary>
		/// Gets the Song at the specified index in the MediaQueue.
		/// </summary>
		/// <param name="index"></param>
		public Song this[int index]
		{
			get { return songQueue[index]; }
		}
		#endregion

		#region Internal Methods
		internal void MoveNext()
		{
			ActiveSongIndex += 1;
		}

		internal void MovePrevious()
		{
			ActiveSongIndex -= 1;
		}

		internal void Play(Song song)
		{
			songQueue.Add(song);
		}

		internal void Play(SongCollection songCollection)
		{
			songQueue.AddRange(songCollection);
		}

		internal void Play(SongCollection songCollection, int index)
		{
			for (int i = index; i < songCollection.Count; i++)
			{
				songQueue.Add(songCollection[i]);
			}
		}
		#endregion
	}
}
